﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using FileManager_Client.BusinessLogic;
using FileManager_Client.BusinessLogic.FileManagerUserService;
using FileManager_Client.Utility;

namespace FileManager_Client.PresentationLayer
{
    public partial class AddMyDepartmentPeople : Form
    {
        private UserService _userService;
        private static object lockWait = new object();
        private WaitDialogForm _waitBeforeLogin;
        private UserLoginResult userSession;
        public List<UserResult> ChoosePeople { get; set; } 
        public AddMyDepartmentPeople()
        {
            InitializeComponent();
            _userService = new UserService();
            ChoosePeople = new List<UserResult>();
            userSession = CacheGlobal.GetCache(GlobalString.System_Config_String.UserSession) as UserLoginResult;
        }

        private void BtnConfirm_Click(object sender, EventArgs e)
        {
            foreach (TreeNode node in tv_MyDepartment.Nodes)
            {
                if (!node.Checked) continue;
                ChoosePeople.Add(node.Tag as UserResult);
            }
        }

        private void AddMyDepartmentPeople_Shown(object sender, EventArgs e)
        {
            tv_MyDepartment.Nodes.Clear();
            var searchCompany = new SearchUserByCompanyCodeArgs
                                    {
                                        SearchUserByCompanyEnumk__BackingField = SearchUserByCompanyEnum.Company,
                                        CodeCollectionk__BackingField = new[] {userSession.CompanyIdk__BackingField}
                                    };
            var backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += backgroundWorker_DoWork;
            backgroundWorker.RunWorkerAsync(searchCompany);
            lock (lockWait)
            {
                _waitBeforeLogin = new WaitDialogForm(GlobalString.System_Config_String.SearchUserStr,
                                                      GlobalString.System_Config_String.MbxInfoStr)
                {
                    BackColor = BackColor
                };
                _waitBeforeLogin.Hide();
            }
        }

        void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            var result = _userService.SearchUserByCompanyCode(e.Argument as SearchUserByCompanyCodeArgs);
            Invoke((EventHandler)delegate
            {
                _waitBeforeLogin.Close();
                foreach (var userResult in result.Resultk__BackingField)
                {
                    var node = new TreeNode
                                   {
                                       ImageIndex = 0,
                                       Text = userResult.UserNamek__BackingField,
                                       Tag = userResult
                                   };
                    tv_MyDepartment.Nodes.Add(node);
                }
                tv_MyDepartment.Update();
            });
        }
    }
}
